#!/usr/bin/env python3

# 这里是8bit ADC模块的相关逻辑，应该是0~5V映射到0~255，用来控制流量阀开度的

import time
from smbus2 import SMBus

# 测试demo，读各口的数据，并将ad部分读的电压配置到da输出端
def ADC_do(addr):
    while True:
        vcc_in = ADC_read(addr, 0x40)
        print('AIN0 = ' + str(vcc_in)) 
        print('AIN1 = ' + str(ADC_read(addr, 0x41))) 
        print('AIN2 = ' + str(ADC_read(addr, 0x42))) 
        with SMBus(1) as bus:
            bus.write_byte_data(addr, 0, vcc_in)
        time.sleep(0.01)

# 输入电压读取
def ADC_read(addr, ctl_byte): 
    with SMBus(1) as bus:
        bus.write_byte(addr, ctl_byte)
        return bus.read_byte_data(addr, 0)

if __name__ == "__main__":
    try:
        ADC_do(0x48)
    except KeyboardInterrupt:
        print("KeyboardInterrupt")